import axios from 'axios'
import { message } from 'antd'
import { getToken, isAuth } from './token'

const http = axios.create({
  baseURL: 'http://geek.itheima.net/v1_0/',
  timeout: 5000
})

http.interceptors.request.use(
  config => {
    if (isAuth()) {
      config.headers.Authorization = `Bearer ${getToken()}`
    }
    return config
  },
  err => Promise.reject(err)
)

http.interceptors.response.use(
  res => res.data,
  err => {
    message.error(err.response.data.message)
    return Promise.reject(err)
  }
)

export { http }
